package org.ourpioneer.excel.excute.process;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.ourpioneer.excel.excute.entity.AllEnum.type1English;
import org.ourpioneer.excel.excute.entity.AllEnum.type1Short;
import org.ourpioneer.excel.excute.entity.FromEntity;
import org.ourpioneer.excel.excute.entity.ToEntity;
import org.ourpioneer.excel.excute.entity.AllEnum.type1;
import org.ourpioneer.excel.excute.entity.AllEnum.type2;
import org.ourpioneer.excel.excute.entity.AllEnum.type3;
import org.ourpioneer.excel.excute.entity.enumList.EnumForList;

public class ProcessFile {
	
	int notFindProf = 0;
	int notFindStage = 0;
	int notFindLanguage = 0;
	int notFindVersion = 0;
	
	//查找项目名称，此为特殊情况
	public String findProjectName(String path) {
		String[] pathArray= path.split("\\\\");
		return pathArray[3];
	}
	
	//查找项目编号，此为特殊情况,在项目名称中，有“-”或者“ ”为分隔符
	public String findProjectCode1(String projectName) {
		if(projectName.contains("-")){
			String[] nameArray= projectName.split("-");
			return nameArray[0];
		}else if(projectName.contains(" ")){
			String[] nameArray= projectName.split(" ");
			return nameArray[0];
		}else{
			return null;
		}
	}
	
	//查找项目编号，文件路径中第四个为项目项目编号
	public String findProjectCode2(String path) {
		String[] pathArray= path.split("\\\\");
		return pathArray[4];
	}
	
	//查找项目编号，文件路径中第三个中的空格之前为项目项目编号
	public String findProjectCode3(String path) {
		String[] pathArray = path.split("\\\\");
		String[] codeAndNameArray = pathArray[3].split(" ");
		return codeAndNameArray[0];
	}
	
	//找寻最大 弃用，单独写
	public String findMax(String whole, int type) {
		String maxType = null;
		int maxCount = 0;
		ArrayList<EnumForList> enumCountlist = new ArrayList<EnumForList>();
		if(type == 1){
			for (type1 subType : type1.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.toString(),-1).length-1);
				if(testEnum.getCount()>0){
					testEnum.setOffset(whole.lastIndexOf(subType.toString()));
				}
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		if(type == 2){
			for (type2 subType : type2.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.toString(),-1).length -1);
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		if(type == 3){
			for (type3 subType : type3.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.toString(),-1).length -1);
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		for (EnumForList enumForList : enumCountlist) {
			int allMax = 0;
			if((enumForList.getCount() == maxCount)&&(maxCount>=1)){
				allMax++;
				maxType = enumForList.getName();
				if(maxType.equals("可研")){
					maxType = "可行性研究";
				}
				if(maxType.equals("初设")){
					maxType = "初级设计";
				}
				if(maxType.equals("规划方案")){
					maxType = "规划设计";
				}
			}
			if(allMax>1){
				System.out.println("错误：有两个或两个以上的并列最大出现次数：" + whole);
				maxType = "不确定";
			}
		}
		if(0==maxCount){
			if((whole.contains("俄"))&&(type == 3)){
				maxType = "俄文";
			}else{
				System.out.println("错误：没有找到枚举中的词：" + whole);
			}
		}
		return maxType;
	}
	
	//找寻专业
	public String findProf(String path,String fileName, int type) {
//		String whole = path + "\\" + fileName;
		String whole = path;
		String maxType = null;
		int maxCount = 0;
		int offset = -1;
		ArrayList<EnumForList> enumCountlist = new ArrayList<EnumForList>();
		if(type == 1){
			for (type1 subType : type1.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.name(),-1).length-1);
				if(testEnum.getCount()>0){
					testEnum.setOffset(whole.lastIndexOf(subType.name()));
				}
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		for (EnumForList enumForList : enumCountlist) {
			if((enumForList.getCount() == maxCount)&&(maxCount>=1)){
				if(enumForList.getOffset() > offset){
					maxType = enumForList.getName();
				}
			}
		}
		if(0==maxCount){
			String profEnglish = findProfEnglish(path,1);
			if(profEnglish == null){
				String profShort = findProfShort(path,1);
				if(profShort == null){
					System.out.println("错误：没有找到专业枚举（type1）中的词：" + path);
				}else{
					maxType = profShort;
				}
			}else{
				maxType = profEnglish;
			}
		}
		return maxType;
	}
	
	//找寻专业英语
	public String findProfEnglish(String whole, int type) {
		String maxType = null;
		int maxCount = 0;
		int offset = -1;
		ArrayList<EnumForList> enumCountlist = new ArrayList<EnumForList>();
		if(type == 1){
			for (type1English subType : type1English.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.name(),-1).length-1);
				if(testEnum.getCount()>0){
					testEnum.setOffset(whole.lastIndexOf(subType.name()));
				}
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		for (EnumForList enumForList : enumCountlist) {
			if((enumForList.getCount() == maxCount)&&(maxCount>=1)){
				if(enumForList.getOffset() > offset){
					maxType = enumForList.getName();
				}
			}
		}
		return maxType;
	}
	
	//找寻专业缩写
	public String findProfShort(String whole, int type) {
		String maxType = null;
		int maxCount = 0;
		int offset = -1;
		ArrayList<EnumForList> enumCountlist = new ArrayList<EnumForList>();
		if(type == 1){
			for (type1Short subType : type1Short.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split("\\\\"+subType.name().toUpperCase()+"-",-1).length-1);
				if(testEnum.getCount()>0){
					testEnum.setOffset(whole.lastIndexOf(subType.name()));
				}else{
					if(whole.endsWith(subType.name())){
						testEnum.setOffset(whole.lastIndexOf(subType.name()));
						testEnum.setCount(1);
					}else{
						testEnum.setCount(whole.split("\\\\"+subType.name().toUpperCase()+" ",-1).length-1);
						if(testEnum.getCount()>0){
							testEnum.setOffset(whole.lastIndexOf(subType.name()));
						}
					}
				}
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		for (EnumForList enumForList : enumCountlist) {
			if((enumForList.getCount() == maxCount)&&(maxCount>=1)){
				if(enumForList.getOffset() > offset){
					maxType = enumForList.getName();
				}
			}
		}
		if(0==maxCount){
			notFindProf++;
		}
		return maxType;
	}
	
	//找寻阶段，全路径（包含文件名），按照出现次数最多
	public String findStage(String whole, int type) {
		String maxType = null;
		int maxCount = 0;
		int offset = -1;
		ArrayList<EnumForList> enumCountlist = new ArrayList<EnumForList>();
		if(type == 2){//选择枚举类型
			for (type2 subType : type2.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.toString(),-1).length-1);
				if(testEnum.getCount()>0){
					testEnum.setOffset(whole.lastIndexOf(subType.toString()));
				}
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		for (EnumForList enumForList : enumCountlist) {
			if((enumForList.getCount() == maxCount)&&(maxCount>=1)){
				if(enumForList.getOffset() > offset){
					maxType = enumForList.getName();
					if(maxType.equals("可研")){
						maxType = "可行性研究";
					}
					if(maxType.equals("初设")){
						maxType = "初级设计";
					}
					if(maxType.equals("规划方案")){
						maxType = "规划设计";
					}
					if(maxType.equals("施工文件")){
						maxType = "施工图";
					}
					if(maxType.equals("竣工文件")){
						maxType = "竣工图";
					}
				}
			}
		}
		if(0==maxCount){
			notFindStage++;
			System.out.println("错误：没有找到阶段枚举（type2）中的词：" + whole);
		}
		return maxType;
	}
	
	//找寻阶段，无文件名，只有路径，按照最后出现
	public String findStageByLast(String whole, int type) {
		String maxType = null;
		int offset = -1;
		ArrayList<EnumForList> enumCountlist = new ArrayList<EnumForList>();
		if(type == 2){//选择枚举类型
			for (type2 subType : type2.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setOffset(-1);
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.toString(),-1).length-1);
				if(testEnum.getCount()>0){
					testEnum.setOffset(whole.lastIndexOf(subType.toString()));
				}
				if(testEnum.getOffset()>offset){
					offset = testEnum.getOffset();
				}
				enumCountlist.add(testEnum);
			}
		}
		for (EnumForList enumForList : enumCountlist) {
			if((enumForList.getOffset() == offset)&&(offset>=-1)){
				maxType = enumForList.getName();
				if(maxType.equals("可研")){
					maxType = "可行性研究";
				}
				if(maxType.equals("初设")){
					maxType = "初级设计";
				}
				if(maxType.equals("规划方案")){
					maxType = "规划设计";
				}
				if(maxType.equals("施工文件")){
					maxType = "施工图";
				}
				if(maxType.equals("竣工文件")){
					maxType = "竣工图";
				}
				if(maxType.equals("设计方案")){
					maxType = "初级设计";
				}
				if(maxType.equals("方案设计")){
					maxType = "初级设计";
				}
			}
		}
		if(-1==offset){
			notFindStage++;
			System.out.println("错误：没有找到阶段枚举（type2）中的词：" + whole);
			return "";
		}
		return maxType;
	}
	
	//找寻语言
	public String findLanguage(String whole, int type) {
		String maxType = null;
		int maxCount = 0;
		ArrayList<EnumForList> enumCountlist = new ArrayList<EnumForList>();
		if(type == 3){
			for (type3 subType : type3.values()) {
				EnumForList testEnum = new EnumForList();
				testEnum.setName(subType.toString());
				testEnum.setCount(whole.split(subType.toString(),-1).length -1);
				if(testEnum.getCount()>maxCount){
					maxCount = testEnum.getCount();
				}
				enumCountlist.add(testEnum);
			}
		}
		for (EnumForList enumForList : enumCountlist) {
			int allMax = 0;
			if((enumForList.getCount() == maxCount)&&(maxCount>=1)){
				allMax++;
				maxType = enumForList.getName();
			}
			if(allMax>1){
				System.out.println("错误：有两个或两个以上的并列最大出现次数：" + whole);
				maxType = "不确定";
			}
		}
		if(0==maxCount){
			if((whole.contains("俄"))&&(type == 3)){
				maxType = "俄文";
			}else{
				notFindLanguage++;
				System.out.println("错误：没有找到语言枚举（type3）中的词：" + whole);
			}
		}
		return maxType;
	}
	
	//查找以“四位数字+年的字符串”，以最早出现的为准
	public String findYear(String whole) {
		String year = null;
		String pattern = "([0-9]{4})年";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(whole);
//		while(m.find()){
//			year = m.group(1);
//		}
		if(m.find()){
			year = m.group(1);
		}
		return year;
	}
	
	//版本
	public String findVersion(String path,String fileName) {//TODO:找到两个报错。
		if(path.contains("PDF版")){
			path = path.replace("PDF版", " ");
		}
		if(fileName.contains("PDF版")){
			fileName = fileName.replace("PDF版", " ");
		}
		String whole = path + "\\" + fileName;
		String version = null;
		boolean findInPath2 = false;
		boolean findInPath1 = false;
		boolean findInPathE1p = false;
		boolean findInPathE1 = false;
		boolean findInPathE2 = false;
		
		String pattern2 = "([A-Z]{2})版";
		Pattern p2 = Pattern.compile(pattern2);
		Matcher m2 = p2.matcher(whole);
		if(m2.find()){
			version = m2.group(m2.groupCount());
			findInPath2 = true;
			if(version.contains("PDF")){
				version = null;
			}else{
				return version;
			}
		}
		
		if(findInPath2==false){
			String pattern1 = "([A-Z0-9]{1})版";
			Pattern p1 = Pattern.compile(pattern1);
			Matcher m1 = p1.matcher(whole);
			if(m1.find()){
				version = m1.group(m1.groupCount());
				findInPath1 = true;
				return version;
			}
		}
		
		if(findInPath1==false){
			String pattern01 = "Rev[\\s|_|\\.]{1}([A-Z]{1}[0-9]{2})";
			Pattern p01 = Pattern.compile(pattern01);
			Matcher m01 = p01.matcher(whole);
			if(m01.find()){
				version = m01.group(m01.groupCount());
				findInPathE1p = true;
				return version;
			}
			String pattern02 = "REV[\\s|_|\\.]{1}([A-Z]{1}[0-9]{2})";
			Pattern p02 = Pattern.compile(pattern02);
			Matcher m02 = p02.matcher(whole);
			if(m02.find()){
				version = m02.group(m02.groupCount());
				findInPathE1p = true;
				return version;
			}
			String pattern03 = "rev[\\s|_|\\.]{1}([A-Z]{1}[0-9]{2})";
			Pattern p03 = Pattern.compile(pattern03);
			Matcher m03 = p03.matcher(whole);
			if(m03.find()){
				version = m03.group(m03.groupCount());
				findInPathE1p = true;
				return version;
			}
		}
		
		if(findInPathE1p==false){
			String pattern01 = "Rev[\\s|_|\\.]{1}([A-Z0-9]{2})";
			Pattern p01 = Pattern.compile(pattern01);
			Matcher m01 = p01.matcher(whole);
			if(m01.find()){
				version = m01.group(m01.groupCount());
				findInPathE1 = true;
				return version;
			}
			String pattern02 = "REV[\\s|_|\\.]{1}([A-Z0-9]{2})";
			Pattern p02 = Pattern.compile(pattern02);
			Matcher m02 = p02.matcher(whole);
			if(m02.find()){
				version = m02.group(m02.groupCount());
				findInPathE1 = true;
				return version;
			}
			String pattern03 = "rev[\\s|_|\\.]{1}([A-Z0-9]{2})";
			Pattern p03 = Pattern.compile(pattern03);
			Matcher m03 = p03.matcher(whole);
			if(m03.find()){
				version = m03.group(m03.groupCount());
				findInPathE1 = true;
				return version;
			}
		}
		
		if(findInPathE2==false){
			String pattern01 = "Rev[\\s|_|\\.]{1}([A-Z0-9]{1})";
			Pattern p01 = Pattern.compile(pattern01);
			Matcher m01 = p01.matcher(whole);
			if(m01.find()){
				version = m01.group(m01.groupCount());
				findInPathE1 = true;
				return version;
			}
			String pattern02 = "REV[\\s|_|\\.]{1}([A-Z0-9]{1})";
			Pattern p02 = Pattern.compile(pattern02);
			Matcher m02 = p02.matcher(whole);
			if(m02.find()){
				version = m02.group(m02.groupCount());
				findInPathE1 = true;
				return version;
			}
			String pattern03 = "rev[\\s|_|\\.]{1}([A-Z0-9]{1})";
			Pattern p03 = Pattern.compile(pattern03);
			Matcher m03 = p03.matcher(whole);
			if(m03.find()){
				version = m03.group(m03.groupCount());
				findInPathE1 = true;
				return version;
			}
		}
		
		if(findInPathE1==false){
			if(fileName.endsWith("AB")){
				version = "AB";
			}else if(fileName.endsWith("A")){
				if(fileName.endsWith(" A")){
					version = "A";
				}else{
					char cTmp = fileName.charAt(fileName.length()-2);
					if(isChinese(cTmp)){
						version = "A";
					}
				}
			}else if(fileName.endsWith("B")){
				if(fileName.endsWith(" B")){
					version = "B";
				}else{
					char cTmp = fileName.charAt(fileName.length()-2);
					if(isChinese(cTmp)){
						version = "B";
					}
				}
			}
			if(version != null){
				return version;
			}
		}
		
		if(version == null){
			notFindVersion++;
		}
		return version;
	}
	
	private boolean isChinese(char c) {
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
			if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
					|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
					|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
					|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
				return true;
			}
			return false;
		}
	
	public ArrayList<Object> extract(List<FromEntity> fromEntitylist) {
		List<ToEntity> toEntitylist = new ArrayList<ToEntity>();
		ArrayList<String> projectNameList = new ArrayList<String>();
		ArrayList<ArrayList<Integer>> projectLine = new ArrayList<ArrayList<Integer>>();
		Integer count = 0;
		for (FromEntity fromEntity : fromEntitylist) {
			ToEntity toEntity = new ToEntity();
			//全路径
			toEntity.setDms_full_path(fromEntity.getFilePath());
			//文件名
			toEntity.setDms_name_en(fromEntity.getFileName());
			//文件编号
			toEntity.setDms_doc_no(fromEntity.getChineseName());
			//题名
			toEntity.setDms_title(fromEntity.getChineseName());
			//年度，基本样式为：D:\国外项目存档\2011年\，从中匹配，必有年
			toEntity.setDms_year(findYear(fromEntity.getFilePath()));
			//-项目编号，D:\国外项目存档\2013年\D-G502-1310  巴格，一般在年份后，可能在项目中文名后，格式为英文数字组合
			toEntity.setDms_project_no(findProjectCode3(fromEntity.getFilePath()));
			//-项目名称
			String projectName = findProjectName(fromEntity.getFilePath());
			if(projectNameList.contains(projectName)){
				int index = projectNameList.indexOf(projectName);
				ArrayList<Integer> tmpIntList = projectLine.get(index);
				tmpIntList.add(count);
				projectLine.set(index, tmpIntList);
			}else {
				projectNameList.add(projectName);
				ArrayList<Integer> tmpIntList = new ArrayList<Integer>();
				tmpIntList.add(count);
				projectLine.add(tmpIntList);
			}
			toEntity.setDms_project_zh(projectName);
			//-项目名称
			toEntity.setDms_project_en(projectName);
			//阶段:枚举 type2
			//按照路径+文件名次数最多
			//toEntity.setDms_stage(findStage(fromEntity.getFilePath()+fromEntity.getFileName(),2));
			//按照路径最后出现
			toEntity.setDms_stage(findStageByLast(fromEntity.getFilePath(),2));
			//专业名称：枚举type1
			toEntity.setDms_prof_zh(findProf(fromEntity.getFilePath(),fromEntity.getFileName(),1));
			//版本：[a-z|A-Z|0-9]+版，getChineseName,"-(0-9)"结尾,"rev或者REV或者Rev+.[ALL]"结尾,
			toEntity.setDms_version(findVersion(fromEntity.getFilePath(),fromEntity.getChineseName()));
			//语言：中文、英文、俄文、俄语、没有中文就提俄
			toEntity.setDms_language(findLanguage(fromEntity.getFilePath()+fromEntity.getFileName(),3));
			//文件类型
			toEntity.setObject_type("design");
			//生命周期
			toEntity.setDms_doc_lifecycle("300");
			//生命周期描述
			toEntity.setDms_doc_lifecycle_label("preArchive");
			//案卷类型
			toEntity.setDms_volume_type("建设项目类(国外)");
			//类别号
			toEntity.setDms_category_code("G02");
			//全宗号
			toEntity.setDms_fondsid("CPE.2");
			//案卷状态
			toEntity.setDms_volume_state("3");
			//案卷状态描述
			toEntity.setDms_volume_state_label("已归档");
			//类型
			toEntity.setDms_type("preArchive");
			//档号(不带后缀文件名)
			toEntity.setDms_archival_code(fromEntity.getChineseName());
			//责任者
			toEntity.setDms_responsible(null);
			//保管期限
			toEntity.setDms_retention_period("永久");
			//载体类型
			toEntity.setDms_carrier_type("纸质");
			//密级
			toEntity.setDms_security_level("无密级");
			//日期
			toEntity.setDms_archive_date(null);
			//档案部门名称
			toEntity.setDms_archive_dept("档案中心");
			//备注
			toEntity.setDms_remark("");
			toEntitylist.add(toEntity);
			count++;
		}
		ArrayList<Object> allInfo = new ArrayList<Object>();
		allInfo.add(toEntitylist);
		allInfo.add(projectNameList);
		allInfo.add(projectLine);
		System.out.println("Size:"+fromEntitylist.size());
		System.out.println("notFindProf:"+notFindProf);
		System.out.println("notFindStage:"+notFindStage);
		System.out.println("notFindLanguage:"+notFindLanguage);
		System.out.println("notFindVersion:"+notFindVersion);
		return allInfo;
	}
}
